package com.laoxu.demo.bootstrapcurd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.demo.bootstrapcurd.common.Result;
import com.laoxu.demo.bootstrapcurd.common.ResultUtil;
import com.laoxu.demo.bootstrapcurd.model.EmqAcl;
import com.laoxu.demo.bootstrapcurd.model.EmqUser;
import com.laoxu.demo.bootstrapcurd.service.EmqAclService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/emqAcl")
@Controller
public class EmqAclController {

    @Autowired
    private EmqAclService emqAclService;

    @RequestMapping
    public String aclView() {
        return "mqtt_acl";
    }


    @GetMapping("/{id}")
    @ResponseBody
    public Result<EmqAcl> get(@PathVariable Integer id) {
        EmqAcl acl = emqAclService.getById(id);
        return ResultUtil.ok(acl);
    }


    @PostMapping("/list")
    @ResponseBody
    public Result<IPage<EmqAcl>> list(@RequestParam(value = "username", required = false) String username,
                                      @RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        // 构造查询条件
        QueryWrapper<EmqAcl> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.like("username", username);
            queryWrapper.orderByDesc("id");
        }
        Page<EmqAcl> page = new Page<>(pageNo, pageSize);
        IPage<EmqAcl> result = emqAclService.page(page, queryWrapper);
        return ResultUtil.ok(result);
    }


    @PostMapping("/add")
    @ResponseBody
    public Result<String> add(@RequestBody EmqAcl acl) {
        emqAclService.save(acl);
        return ResultUtil.ok("添加成功！");
    }


    @PostMapping("/modify")
    @ResponseBody
    public Result<String> modify(@RequestBody EmqAcl acl) {
        emqAclService.saveOrUpdate(acl);
        return ResultUtil.ok("修改成功！");
    }


    @PostMapping("/remove")
    @ResponseBody
    public Result<String> remove(@RequestParam Integer id) {
        emqAclService.removeById(id);
        return ResultUtil.ok("删除成功！");
    }


}
